package com.softline.documentsmall.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softline.documentsmall.entity.RelateBook;
import com.softline.documentsmall.ret.ReturnVO;
import com.softline.documentsmall.ret.ReturnVOUtils;
import com.softline.documentsmall.service.RelateBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @Author wph
 * @Date 2021/1/27 17:18
 */
@Api(value = "后台管理___图书接口", tags = {"后台管理___图书接口"})
@RestController
@RequestMapping("/extend/relateBook")
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
public class RelateBookController {

    @Autowired
    RelateBookService relateBookService;

    @ApiOperation(value = "查询图书列表", notes = "查询图书列表")
    @GetMapping(value = "/list")
    public ReturnVO list(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,  @RequestParam(name = "dicId", required = false) Integer dicId, @RequestParam(name = "title", required = false) String title) {
        Page<RelateBook> result = new Page<>();
        List<RelateBook> records = relateBookService.selectList(title, dicId, pageNo, pageSize);
        Long count = Long.valueOf(relateBookService.selectCountLoacl(title, dicId));
        result.setRecords(records);
        result.setCurrent(pageNo);
        result.setSize(pageSize);
        result.setTotal(count);
        return ReturnVOUtils.success(result);
    }

    @ApiOperation(value = "查询图书详情", notes = "查询图书详情")
    @GetMapping(value = "/selectById")
    public RelateBook selectById(@RequestParam(name = "id") int id) {
        return relateBookService.selectById(id);
    }

    @ApiOperation(value = "新增图书", notes = "新增图书")
    @PostMapping(value = "/insert")
    public ReturnVO insert( @RequestBody RelateBook relateBook) {
        return relateBookService.insert(relateBook);
    }

    @ApiOperation(value = "修改图书", notes = "修改图书")
    @PostMapping(value = "/update")
    public ReturnVO update( @RequestBody RelateBook relateBook) {
        return relateBookService.updateRelateBookById(relateBook);
    }

}
